package org.swingDemo.UI.panel;

import org.swingDemo.util.IconUtil;
import org.swingDemo.util.UIUtil;

import javax.swing.*;
import java.awt.*;
import java.io.File;

public class FileItemPanel extends JPanel {

    private File file;

    private JLabel iconLabel;
    private JLabel nameLabel;

    public FileItemPanel(File file) {
        this.file = file;
        initPanel();
    }

    private void initPanel() {
        ImageIcon icon = IconUtil.FolderIcon;
        String name = "上一级";
        if (file != null) {
            icon = file.isDirectory() ? IconUtil.FolderIcon : new ImageIcon(file.getAbsolutePath());
            name = this.file.getName();
        }
        iconLabel = UIUtil.createIconLabel(icon);
        nameLabel = UIUtil.createLabel(name);
        nameLabel.setForeground(Color.cyan);

        this.setBorder(BorderFactory.createLineBorder(Color.gray));
        this.setLayout(new BorderLayout());
        this.add(iconLabel, BorderLayout.CENTER);
        this.add(nameLabel, BorderLayout.PAGE_END);
    }
}
